import Vue from 'vue'
import Vuex from 'vuex'
import { setItem, getItem, removeItem } from '@/utils/storage'

Vue.use(Vuex)
// 变量是小写的，常量是大写 书写规范
const TOUTIAO_USER = 'TOUTIAO_USER'
export default new Vuex.Store({
  // 数据源仓库，类似于Vue对象里面的data
  state: {
    // user: null
    user: getItem(TOUTIAO_USER)
    // user: JSON.parse(window.localStorage.getItem(TOUTIAO_USER))
  },
  mutations: {
    // 登录成功后将token存入vuex和localStorage
    setUser (state, user) {
      state.user = user
      // 本地存储只能存字符串
      setItem(TOUTIAO_USER, user)
      // window.localStorage.setItem(TOUTIAO_USER, JSON.stringify(user))
    },
    // 退出登录user置空，删除localStorage里的token
    outUser (state) {
      state.user = null
      removeItem(TOUTIAO_USER)
    }
  },
  actions: {
  },
  modules: {
  }
})
